package me.tonywang.common.util;

import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.util.StringUtils;

import java.sql.SQLException;
import java.util.*;

import static java.util.stream.Collectors.toList;

/**
 * <p>
 *
 * @author wanghui
 * @since 2018/2/2 11:10
 */
public class DataX {

    @Test
    public  void test() throws SQLException {

        Properties  drivers = new Properties();
        drivers.put("","");

        String readerUrl ="jdbc:sqlserver://192.168.3.3:1433;DatabaseName=P930_ISSUE";
        String username="sa";
        String password ="123";
        //DriverManager会自动查找驱动JAR包是否存在，存在则自动使用该驱动 见DriverManager.getConnection
        DriverManagerDataSource  readerDataSource = new DriverManagerDataSource(  readerUrl,   username,   password);
        System.out.println(readerDataSource.getConnection());
        JdbcTemplate jdbcTemplate = new JdbcTemplate(readerDataSource);
        List<Map<String, Object>> readerResult = jdbcTemplate.queryForList("select * from t_role");


        //
        String writeUrl ="jdbc:mysql://192.168.3.3:3307/jzetech_issue?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&useSSL=false\n";
        DriverManagerDataSource  writerDataSource = new DriverManagerDataSource(  writeUrl,   "purchase",   "123456");
        JdbcTemplate jdbcTemplate1 = new JdbcTemplate(writerDataSource);

        if(readerResult!= null && !readerResult.isEmpty() ){
            List<String> columnSet = new ArrayList<>(readerResult.get(0).keySet());
            List<String> qSet  = columnSet.stream().map(d->   "?" ).collect(toList());
            Object[] values  = columnSet.stream().map(d->   "1" ).collect(toList()).toArray();
            String columnSql = StringUtils.arrayToCommaDelimitedString(columnSet.toArray());
            String questionSql = StringUtils.arrayToCommaDelimitedString(qSet.toArray());
            System.out.println(columnSql);  System.out.println(questionSql); System.out.println(values);
//            String sql = " insert into qp_supplier_info ( "+columnSql+
//                          " ) values ("+ questionSql+")";
//              List<Object[]> batchArgs =  new ArrayList<>();
//            batchArgs.add(values);
//            jdbcTemplate1.batchUpdate(sql,batchArgs);
        }

    }
}
